package com.j.lemon.learn.thread.lock;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @Author lijunjun
 * @Date 2019-04-22 13:59
 * @Description 循环屏障 （七龙珠）
 */
public class CyclicBarrierDemo {
    private CyclicBarrier cyclicBarrier = new CyclicBarrier(7, () -> {
        System.out.println("召唤神龙!");
    });


    public void wait1() throws BrokenBarrierException, InterruptedException {
        System.out.println(Thread.currentThread().getName()+"拿到一颗龙珠");
        cyclicBarrier.await();
    }

    public static void main(String[] args) {
        CyclicBarrierDemo cyclicBarrierDemo = new CyclicBarrierDemo();

        for (int i = 0; i < 20; i++) {
            new Thread(()->{
                try {
                    cyclicBarrierDemo.wait1();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            },String.valueOf(i)).start();
        }

    }
}
